package com.oopnet.template.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

public class TranslateLanguage {
	
	private static TranslateLanguage instance = new TranslateLanguage();
	
	private static Map<String, String> EN_LANGUAGE_MAP = new ConcurrentHashMap<String, String>();

	private static Map<String, String> CH_LANGUAGE_MAP = new ConcurrentHashMap<String, String>();
	
	private TranslateLanguage(){}
	
	private static Logger log = Logger.getLogger(TranslateLanguage.class);
	
	public static TranslateLanguage getInstance(){
		return instance;
	}
	
	/**
	 * Initial English Source File when class initialization. (default English)
	 */
	static{
		Properties prop = new Properties();
		InputStream EN_Stream;
		try {
			log.info("Initial English source file...");
			File f = new File(TranslateLanguage.class.getResource("/").getPath());
			EN_Stream = new FileInputStream(
					new File(f.getParentFile().getParentFile().getPath()+"/language/sfdcresourcebundle_en_US.properties"));
			prop.load(EN_Stream);
			Set<Object> keySet = prop.keySet();

			for (Object object : keySet) {
				String key = (String) object;
				EN_LANGUAGE_MAP.put(prop.getProperty(key).trim(), key.trim());
			}
			EN_Stream.close();
			prop.clear();
		}  catch (IOException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
	}
	
	
	/**
	 * Translate target massage.
	 * @param message
	 * @param language
	 * @return
	 */
	public String translate(String message,String language){
		String result = "";
		String translateKey = EN_LANGUAGE_MAP.get(message.trim());
		log.info("translateKey: "+translateKey);
		if(language.trim().equals("zh_CN")){
			if(CH_LANGUAGE_MAP.isEmpty()){
				log.info("Loading Language Source File...");
				CH_LANGUAGE_MAP = readSourceFile2Map(language);
			}
			result = CH_LANGUAGE_MAP.get(translateKey);
			log.info("result: "+result);
		}
		
		return result;
	}
	
	/**
	 * Initial the language file when change the locale language.(run time)
	 * @param language
	 * @return
	 */
	private synchronized Map<String, String> readSourceFile2Map(String language){
		
		Map<String, String> languageMap = new HashMap<String ,String>();
		Properties prop = new Properties();
		InputStream input;
		try {
			File f = new File(TranslateLanguage.class.getResource("/").getPath());
			input = new FileInputStream(
					new File(f.getParentFile().getParentFile().getPath()+"/language/sfdcresourcebundle_"+language+".properties"));
			prop.load(input);
			Set<Object> keySet = prop.keySet();

			for (Object object : keySet) {
				String key = (String) object;
				languageMap.put( key.trim(),prop.getProperty(key).trim());
			}
			input.close();
			prop.clear();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return languageMap;
	}
	
	public static void main(String[] args) {
		System.out.println("AAAAA\r\nBBBBBB");
	}
	
}
